package cn.wizzer.app.crown.commons.websocket.main.base;

import io.netty.channel.Channel;
import lombok.Data;
import org.nutz.lang.Strings;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 22099
 */
@Data
public class NettyRouter {

    private static final Map emptyMap = new HashMap(0);

    private String uri;

    private Map<String, String> params = emptyMap;

    private String[] uriPart;

    private Channel income;



    public NettyRouter(String uri) {
        this.setUri(uri);
    }

    public void setUri(String uri) {
        //包含参数-识别
        if (uri.contains("?")) {
            String[] split = uri.split("\\?");
            this.uri = split[0];
            this.uriPart = split[0].split("/");
            if (Strings.isNotBlank(split[1])) {
                String[] paramStrings = split[1].split("&");
                params = new HashMap(paramStrings.length);
                for (String param : paramStrings) {
                    String[] strings = param.split("=");
                    params.put(strings[0], strings[1]);
                }
            }
        } else {
            this.uri = uri;
        }
    }

    public String getUriPart(int index) {
        if (this.uriPart.length <= index) {
            return null;
        }
        return uriPart[index];
    }

    public String getParam(String key) {
        return this.getParams().get(key);
    }
}
